package org.bikelab.sephiroce.hbase;

import java.io.IOException;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseTest {
	static String tablename;
	public static void main(String[] args) throws IOException {
		tablename = args[0];
		createTable();
		checkHBase();
	  }
	
	public static void createTable()  throws IOException{
		HBaseConfiguration conf = new HBaseConfiguration();
		conf.set("hbase.master","10.25.3.22:54310");
		 
		HBaseAdmin hbase = new HBaseAdmin(conf);
		//table name
		HTableDescriptor desc = new HTableDescriptor(tablename);
		//column
		desc.addFamily(new HColumnDescriptor("ntriple".getBytes()));
		//column
		hbase.createTable(desc);
	}
	
	public static void checkHBase() throws IOException{
		 // You need a configuration object to tell the client where to connect.
	    // When you create a HBaseConfiguration, it reads in whatever you've set
	    // into your hbase-site.xml and in hbase-default.xml, as long as these can
	    // be found on the CLASSPATH
		HBaseConfiguration conf = new HBaseConfiguration();
		conf.set("hbase.master","10.25.3.22:54310");

	    // This instantiates an HTable object that connects you to
	    // the "myLittleHBaseTable" table.
	    HTable table = new HTable(conf, tablename);

	    // To add to a row, use Put.  A Put constructor takes the name of the row
	    // you want to insert into as a byte array.  In HBase, the Bytes class has
	    // utility for converting all kinds of java types to byte arrays.  In the
	    // below, we are converting the String "myLittleRow" into a byte array to
	    // use as a row key for our update. Once you have a Put instance, you can
	    // adorn it by setting the names of columns you want to update on the row,
	    // the timestamp to use in your update, etc.If no timestamp, the server
	    // applies current time to the edits.
	    //setting key
	    Put p = new Put(Bytes.toBytes("<http://cbio.mskcc.org/cpath#CPATH-LOCAL-16802163>"));

	    // To set the value you'd like to update in the row 'myLittleRow', specify
	    // the column family, column qualifier, and value of the table cell you'd
	    // like to update.  The column family must already exist in your table
	    // schema.  The qualifier can be anything.  All must be specified as byte
	    // arrays as hbase is all about byte arrays.  Lets pretend the table
	    // 'myLittleHBaseTable' was created with a family 'myLittleFamily'.
	    p.add(Bytes.toBytes("ntriple"), Bytes.toBytes("someQualifier"), Bytes.toBytes("<http://cbio.mskcc.org/cpath#CPATH-LOCAL-16802163> <http://www.biopax.org/release/biopax-level2.owl#ID> \"MI:0496\""));
	    
	    // Once you've adorned your Put instance with all the updates you want to
	    // make, to commit it do the following (The HTable#put method takes the
	    // Put instance you've been building and pushes the changes you made into
	    // hbase)
	    table.put(p);
	    table.close();
	    conf.clear();
	}
}